<template>
  <div class="loading-container" :class="[`loading-${type}`, { 'is-fullscreen': fullscreen }]">
    <div class="loading-content">
      <!-- 自定义加载动画 -->
      <div v-if="type === 'custom'" class="custom-loading">
        <div class="loading-spinner">
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
        </div>
      </div>

      <!-- Element Plus 加载动画 -->
      <div v-else-if="type === 'element'" class="element-loading">
        <el-icon class="is-loading" :size="iconSize">
          <Loading />
        </el-icon>
      </div>

      <!-- 脉冲加载动画 -->
      <div v-else-if="type === 'pulse'" class="pulse-loading">
        <div class="pulse-dot"></div>
        <div class="pulse-dot"></div>
        <div class="pulse-dot"></div>
      </div>

      <!-- 波浪加载动画 -->
      <div v-else-if="type === 'wave'" class="wave-loading">
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
      </div>

      <!-- 进度条加载 -->
      <div v-else-if="type === 'progress'" class="progress-loading">
        <div class="progress-circle">
          <svg viewBox="0 0 100 100">
            <circle
              cx="50"
              cy="50"
              r="45"
              fill="none"
              stroke="#f0f0f0"
              stroke-width="8"
            />
            <circle
              cx="50"
              cy="50"
              r="45"
              fill="none"
              stroke="#409EFF"
              stroke-width="8"
              stroke-linecap="round"
              :stroke-dasharray="circumference"
              :stroke-dashoffset="progressOffset"
              class="progress-circle-fill"
            />
          </svg>
          <div class="progress-text">
            <span class="progress-value">{{ Math.round(progress) }}%</span>
          </div>
        </div>
      </div>

      <!-- 骨架屏加载 -->
      <div v-else-if="type === 'skeleton'" class="skeleton-loading">
        <div class="skeleton-item">
          <div class="skeleton-avatar"></div>
          <div class="skeleton-content">
            <div class="skeleton-line skeleton-title"></div>
            <div class="skeleton-line skeleton-text"></div>
            <div class="skeleton-line skeleton-text short"></div>
          </div>
        </div>
        <div class="skeleton-item">
          <div class="skeleton-avatar"></div>
          <div class="skeleton-content">
            <div class="skeleton-line skeleton-title"></div>
            <div class="skeleton-line skeleton-text"></div>
          </div>
        </div>
      </div>

      <!-- 文件上传加载 -->
      <div v-else-if="type === 'upload'" class="upload-loading">
        <div class="upload-icon">
          <el-icon :size="iconSize" color="#409EFF">
            <UploadFilled />
          </el-icon>
        </div>
        <div class="upload-progress">
          <el-progress
            :percentage="progress"
            :stroke-width="6"
            :show-text="false"
          />
        </div>
      </div>

      <!-- 默认加载动画 -->
      <div v-else class="default-loading">
        <div class="loading-dots">
          <div class="dot"></div>
          <div class="dot"></div>
          <div class="dot"></div>
        </div>
      </div>

      <!-- 加载文本 -->
      <div v-if="text" class="loading-text">
        <p class="loading-message">{{ text }}</p>
        <p v-if="subText" class="loading-sub-message">{{ subText }}</p>
      </div>

      <!-- 估计剩余时间 -->
      <div v-if="showEstimatedTime && estimatedTime > 0" class="estimated-time">
        <p>预计剩余时间：{{ formatTime(estimatedTime) }}</p>
      </div>

      <!-- 取消按钮 -->
      <div v-if="showCancel" class="loading-cancel">
        <el-button type="text" @click="handleCancel">
          取消
        </el-button>
      </div>
    </div>

    <!-- 背景遮罩 -->
    <div v-if="showMask" class="loading-mask"></div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Loading, UploadFilled } from '@element-plus/icons-vue'

const props = defineProps({
  // 加载类型
  type: {
    type: String,
    default: 'default', // default, custom, element, pulse, wave, progress, skeleton, upload
    validator: (value) => [
      'default', 'custom', 'element', 'pulse', 'wave', 'progress', 'skeleton', 'upload'
    ].includes(value)
  },
  // 加载文本
  text: {
    type: String,
    default: ''
  },
  // 副文本
  subText: {
    type: String,
    default: ''
  },
  // 进度值 (0-100)
  progress: {
    type: Number,
    default: 0
  },
  // 预计剩余时间（秒）
  estimatedTime: {
    type: Number,
    default: 0
  },
  // 图标大小
  iconSize: {
    type: [String, Number],
    default: 40
  },
  // 是否全屏
  fullscreen: {
    type: Boolean,
    default: false
  },
  // 是否显示背景遮罩
  showMask: {
    type: Boolean,
    default: true
  },
  // 是否显示预计时间
  showEstimatedTime: {
    type: Boolean,
    default: false
  },
  // 是否显示取消按钮
  showCancel: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['cancel'])

// 计算属性
const circumference = computed(() => 2 * Math.PI * 45)

const progressOffset = computed(() => {
  return circumference.value - (props.progress / 100) * circumference.value
})

// 格式化时间
const formatTime = (seconds) => {
  if (seconds < 60) {
    return `${seconds} 秒`
  } else if (seconds < 3600) {
    const minutes = Math.floor(seconds / 60)
    const remainingSeconds = seconds % 60
    return remainingSeconds > 0 ? `${minutes} 分 ${remainingSeconds} 秒` : `${minutes} 分`
  } else {
    const hours = Math.floor(seconds / 3600)
    const minutes = Math.floor((seconds % 3600) / 60)
    return minutes > 0 ? `${hours} 小时 ${minutes} 分` : `${hours} 小时`
  }
}

// 取消事件
const handleCancel = () => {
  emit('cancel')
}
</script>

<style scoped>
.loading-container {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 2rem;
}

.loading-container.is-fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  z-index: 1;
}

.loading-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  z-index: 0;
}

/* 自定义加载动画 */
.custom-loading {
  position: relative;
  width: 60px;
  height: 60px;
}

.loading-spinner {
  position: relative;
  width: 100%;
  height: 100%;
}

.spinner-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 2px solid transparent;
  border-top: 2px solid #409EFF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.spinner-ring:nth-child(2) {
  width: 50px;
  height: 50px;
  margin: -25px 0 0 -25px;
  border-top-color: #67C23A;
  animation-duration: 1.5s;
  animation-direction: reverse;
}

.spinner-ring:nth-child(3) {
  width: 60px;
  height: 60px;
  margin: -30px 0 0 -30px;
  border-top-color: #E6A23C;
  animation-duration: 2s;
}

/* Element Plus 加载动画 */
.element-loading {
  color: #409EFF;
}

/* 脉冲加载动画 */
.pulse-loading {
  display: flex;
  gap: 8px;
}

.pulse-dot {
  width: 12px;
  height: 12px;
  background: #409EFF;
  border-radius: 50%;
  animation: pulse 1.4s ease-in-out infinite both;
}

.pulse-dot:nth-child(1) { animation-delay: -0.32s; }
.pulse-dot:nth-child(2) { animation-delay: -0.16s; }

/* 波浪加载动画 */
.wave-loading {
  display: flex;
  gap: 4px;
  align-items: flex-end;
}

.wave-bar {
  width: 6px;
  height: 30px;
  background: #409EFF;
  border-radius: 3px;
  animation: wave 1.2s ease-in-out infinite;
}

.wave-bar:nth-child(2) { animation-delay: 0.1s; }
.wave-bar:nth-child(3) { animation-delay: 0.2s; }
.wave-bar:nth-child(4) { animation-delay: 0.3s; }
.wave-bar:nth-child(5) { animation-delay: 0.4s; }

/* 进度条加载 */
.progress-loading {
  position: relative;
}

.progress-circle {
  position: relative;
  width: 80px;
  height: 80px;
}

.progress-circle svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.progress-circle-fill {
  transition: stroke-dashoffset 0.3s ease;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.progress-value {
  font-size: 14px;
  font-weight: 600;
  color: #409EFF;
}

/* 骨架屏加载 */
.skeleton-loading {
  width: 300px;
}

.skeleton-item {
  display: flex;
  margin-bottom: 20px;
}

.skeleton-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s infinite;
}

.skeleton-content {
  flex: 1;
  margin-left: 16px;
}

.skeleton-line {
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s infinite;
  margin-bottom: 8px;
}

.skeleton-title {
  height: 16px;
  width: 80%;
}

.skeleton-text {
  width: 100%;
}

.skeleton-text.short {
  width: 60%;
}

/* 文件上传加载 */
.upload-loading {
  min-width: 200px;
}

.upload-icon {
  margin-bottom: 16px;
}

.upload-progress {
  width: 100%;
}

/* 默认加载动画 */
.default-loading {
  display: flex;
  align-items: center;
}

.loading-dots {
  display: flex;
  gap: 8px;
}

.dot {
  width: 8px;
  height: 8px;
  background: #409EFF;
  border-radius: 50%;
  animation: bounce 1.4s ease-in-out infinite both;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }

/* 加载文本 */
.loading-text {
  margin-top: 16px;
}

.loading-message {
  margin: 0 0 8px 0;
  color: #606266;
  font-size: 14px;
  font-weight: 500;
}

.loading-sub-message {
  margin: 0;
  color: #909399;
  font-size: 12px;
}

/* 估计时间 */
.estimated-time {
  margin-top: 12px;
}

.estimated-time p {
  margin: 0;
  color: #909399;
  font-size: 12px;
}

/* 取消按钮 */
.loading-cancel {
  margin-top: 16px;
}

/* 动画定义 */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes wave {
  0%, 40%, 100% {
    transform: scaleY(0.4);
  }
  20% {
    transform: scaleY(1);
  }
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.7;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes skeleton-loading {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .loading-container {
    padding: 1rem;
  }
  
  .skeleton-loading {
    width: 250px;
  }
  
  .upload-loading {
    min-width: 150px;
  }
}
</style>